
package org.streets.eis.ext.analysis.internal.impl;
import org.slf4j.Logger;
import org.streets.database.SQLConnection;
import org.streets.database.datadict.DataDict;
import org.streets.database.datadict.TableRelations;
import org.streets.eis.ext.analysis.entities.QueryStruct;
import org.streets.eis.ext.analysis.internal.buildlogic.QueryBuilderLogic;
import org.streets.eis.ext.analysis.internal.buildlogic.QueryBuilderLogicImpl;
import org.streets.eis.ext.analysis.services.QueryBuilder;

public class QueryBuilderImpl implements QueryBuilder {
	private static final long serialVersionUID = 1L;
	private QueryBuilderLogic _builderLogic;
	//private SqlStruct _sqlStruct;
	private QueryStruct _queryEntity;	
	
	public QueryBuilderImpl() {
		_builderLogic = new QueryBuilderLogicImpl();
		//_sqlStruct = new SqlStruct();
		_queryEntity = new QueryStruct();
	}
	
	public QueryBuilderImpl(Logger logger, DataDict dict,TableRelations relations, SQLConnection connection) {
		_builderLogic = new QueryBuilderLogicImpl(logger,dict,relations,connection);
		//_sqlStruct = new SqlStruct();
		_queryEntity = new QueryStruct();
	}
	
	public QueryBuilder addCondition(String... conditions) {
		for(String condition : conditions){
			_builderLogic.appendWhereField(_queryEntity,condition);
		}
		
		return this;
	}
	
	
	public QueryBuilder addConditionExpr(String expr) {
		_queryEntity.setConditionExpr(expr);
		return this;
	}

	public QueryBuilder addFields(String... fieldNames) {
		for(String fieldName : fieldNames){
			_builderLogic.appendDisplayField(_queryEntity,fieldName);
		}
		
		return this;
	}


	public QueryBuilder addOrderBy(String fieldName) {
		addOrderBy(fieldName, "ASC");
		return this;
	}

	
	public QueryBuilder addOrderBy(String fieldName, String orderType) {
		_builderLogic.appendOrderField(_queryEntity, fieldName,orderType);
		
		return this;
	}

	
	public QueryBuilder addTable(String name, String alias) {
		addTable(name,alias,false);
		return this;
	}

	public QueryBuilder addTable(String name,String alias,Boolean allShow){				
		_builderLogic.appendTable(_queryEntity,name,alias,allShow);
		return this;
	}

	public QueryStruct build() {
		//TODO:如何将SqlStruct和QueryEntity统一起来？？？？这样就完美了
		return _queryEntity;		
	}

 
 
}
